<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="#">
<!--        pattern：匹配到一个就结束-->
        <input type="text" name="country_code" pattern="[a-zA-Z0-9]" title="请输入字母和数字">
        <button>提交</button>
    </form>

    <script>
        // let reg = /abc/;//要求输入字符必须是abc
        // let reg = new RegExp(/abc/);
        // let reg = new RegExp("abc");
        // let reg = RegExp("abc");
        let reg = RegExp(/abc/);//包含abc
        //正则表达式，主要用来验证字符串，可以搜索和替换等
        // let s = "abc";
        //验证方法
        console.log(reg.test("abc"));
        console.log(reg.test("s"));
        console.log(reg.test("a"));
        console.log(reg.test("abcd"));
        console.log(reg.test("aabcd"));
        //exec()：会将符合表达式的字符串保存到一个数组中RegExpExecArray
        console.log(reg.exec("aabcd"));
        //要求输入的字符只能是abc
        reg = /^abc$/;
        console.log(reg.test("abc"));
        console.log(reg.test("a"));
        console.log(reg.test("abcd"));
        console.log(reg.test("aabcd"));

        //范围搜索
        //只要有abc其中一个字符即可
        // reg = /[abc]/;
        // reg = /[cab]/;
        // reg = /c|a|b/;//选择项比较少
        //只要包含a~z中的任意字符即可
        // reg = /[a-z]/;
        // reg = /a-z/;//匹配包含a-z的字符串
        //支持大小写a~z
        // reg = /[a-zA-Z]/;
        //支持大小字母数字0-9和_和-
        // reg = /[a-zA-Z0-9_-]/;
        //取反
        // reg = /[^a-zA-Z0-9_-]/;
        //以a开头，以c结尾，即可
        // reg = /^a.c$/;//.只会匹配一个字符
        //以^，以$结尾
        reg = /^\^.\$$/;
        console.log(`reg.test("^1$")：${reg.test("^1$")}`);
        console.log(`reg.test("abc")：${reg.test("abc")}`);
        console.log(`reg.test("abc")：${reg.test("a1c")}`);
        console.log(`reg.test("a11c")：${reg.test("a11c")}`);
        console.log(`reg.test("a")：${reg.test("a")}`);
        console.log(`reg.test("abcd")：${reg.test("abcd")}`);
        console.log(`reg.test("aabcd")：${reg.test("aabcd")}`);
        console.log(`reg.test("hijk")：${reg.test("hijk")}`);
        console.log(`reg.test("123")：${reg.test("123")}`);
        console.log(`reg.test("ABC")：${reg.test("ABC")}`);
        console.log(`reg.test("ABC123xyz")：${reg.test("ABC123xyz")}`);
        console.log(`reg.test("你好")：${reg.test("你好")}`);
        //要求：写一个正则表达式，实现表单账号输入框的内容，只能包含大小字母和数字

        //量词符
        //要求：密码必须6位或以上
        reg = /^.{6,}$/;
        console.log(`reg.test("你好111111111")：${reg.test("你好111111111")}`);
        console.log(`reg.test("你好")：${reg.test("你好")}`);

        //要求：密码必须6位到20位
        reg = /^.{6,20}$/;
        //要求：手机必须是11位数字
        // reg = /^[0-9]{11}$/;
        reg = /^\d{11}$/;
        //国内手机号
        reg = /^1[34589][0-9]{9}/;
        console.log(`reg.test("你好111111111")：${reg.test("你好111111111")}`);
        console.log(`reg.test("13111111111")：${reg.test("13111111111")}`);
        //要求：账号必须以_开头，中间包含a
        reg = /^_.*a+/;
        console.log(`reg.test("_你好111111111")：${reg.test("_你好111111111")}`);
        console.log(`reg.test("_你好11a111")：${reg.test("_你好11a111")}`);
        console.log(`reg.test("_你好11a11aa1")：${reg.test("_你好11a11aa1")}`);

        console.log("\t1\r2\n3\v4\f5");

        let s = "foo1 foo2 bar foo3 FOO4";
        //取出foo单词
        // reg = /(foo.)/;//非全局匹配
        reg = /(foo.)/gi;//全局匹配
        //exec在非全局匹配的模式下，只返回第一次匹配到的内容
        console.log(reg.exec(s));
        // console.log(reg.exec(s));
        // console.log(reg.exec(s));
        // console.log(reg.exec(s));
        // console.log(s.match(reg));
        //剩余的匹配项，就会保存在下面$n的变量中
        console.log(RegExp.$1);
        console.log(RegExp.$2);
        console.log(RegExp.$3);
        console.log(RegExp.$4);
        // let result;
        // while ((result = reg.exec(s)) != null)  {
        //     console.log(result);
        //     console.log(reg.lastIndex);
        // }

        reg = /industr(?:y|ies)/;
        s = "industry这个单词是什么意思?";
        console.log(reg.test(s));
        console.log(reg.exec(s));
        s = "industries这个单词是什么意思?";
        console.log(reg.test(s));
        console.log(reg.exec(s));

        reg = /Windows(?=95|98|NT|2000)/i;
        console.log(reg.exec("windows2000"));
        console.log(reg.exec("windows98"));
        console.log(reg.exec("98"));
        console.log(reg.exec("windows3.1"));

        //提取话题
        s = "#这是一个话题内容#";
        reg = /^#(.*)#$/;
        console.log(reg.test(s));
        console.log(RegExp.$1);
        // console.log(s.match(reg));

        s = "9-21*5/3+18/3*9/9*2+10*568/14";
        //先做乘除再做加减
        reg = /(\d+[*/])+\d+/g;
        // console.log(reg.exec(s));
        // console.log(RegExp.$1.txt);
        // console.log(reg.exec(s));
        let a = s.match(reg);
        console.log(a);
        //21*5/3  21  *   5  /   3
        // s = "21*5/3";
        // reg = /(\d+)|([*/])/g;
        // console.log(reg.exec(s));
        // console.log(reg.exec(s));
        // console.log(reg.exec(s));
        // let a1 = s.match(reg);
        // console.log(a1);
        // console.log(a1[0]);
        // console.log(a1[1.txt]);
        // if (a1[1.txt] == "*") {
        //     let re = a1[0] * a1[2];
        // }
        a.forEach(value => {
            let reg = /(\d+)|([*/])/g;
            let result = t1(value.match(reg));
            console.log(result);
            s = s.replace(value, result);
        });
        console.log("s:" + s);

        function t1(a1) {
            let mark;
            let markIndex = 0;
            let result;
            for (let i = 0; i < a1.length; i+=2) {
                markIndex = i + 1;
                mark = a1[markIndex];
                // console.log("markIndex:" + markIndex);
                // console.log("mark:" + mark);
                if (!result) {
                    result = a1[markIndex - 1];
                }
                // console.log("result:" + result);
                // console.log("a1[markIndex + 1.txt]:" + a1[markIndex + 1.txt]);
                if (mark == "*") {
                    result = result * a1[markIndex + 1];
                } else if (mark == "/") {
                    result = result / a1[markIndex + 1];
                }
                // console.log("result:" + result);
            }
            console.log("result:" + result);
            return result;
        }

        s = "hello javascript";
        reg = /(?:\w+)\s(\w+)/;
        reg.test(s);
        console.log(RegExp.$1);
        console.log(RegExp.$2);
        console.log(s.replace(reg, "$2 $1"));
        s = "兵马未动，粮草先行";
        console.log(s.replace("草", "*"));
    </script>
</body>
</html>